package com.powernode.controller;

import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSON;
import com.powernode.dto.Result;
import com.powernode.service.AliPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@RestController
@RequestMapping("/oms/pay")
public class AlipayController {

    @Autowired
    private AliPayService aliPayService;
    @Autowired
    private HttpServletResponse response;

    /**
     * 跳转到手机网站支付页面
     */
    @GetMapping("/{orderId}")
    public void toPayWap(@PathVariable Long orderId) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        // 将数据响应给客户端
        Result result = aliPayService.toPayWap(orderId);
        if (result.getCode() == HttpStatus.HTTP_OK){
            response.setContentType("text/html;charset=UTF-8");
            writer.write((String) result.getData());
        }
        writer.write(JSON.toJSONString(result));
    }

    /**
     * 支付同步回调
     */
    @GetMapping("/success")
    public void success(@RequestParam Map<String, String> params){
        aliPayService.success(params);
    }
    /**
     * 支付异步回调
     */
    @PostMapping("/notify")
    public void notify(@RequestParam Map<String, String> params){
        aliPayService.notify(params);
    }
}
